
#!/bin/bash

echo "Conecting to the database.."
bucket=/mnt/s3bucket/
dir=$bucket"ProcessedLogs"
for file in "$dir"/*; do
	echo "------------------------"$file"-----------------------------------------"
	intermediate=${file#*'/mnt/s3bucket/ProcessedLogs/'}	
	echo "$intermediate"
	if [ "$intermediate" == "toDatabase" ]; then
        mkdir $file/akn_session_fact $file/akn_traffic_fact $file/akn_word_fact $file/akn_query_fact $file/akn_noresultquery_fact
		for subfile in "$file"/*; do	
			intermediateFile=${subfile#*'/mnt/s3bucket/ProcessedLogs/'}
			subfolderName=${intermediateFile#*'/'}
			echo "SubfolderName path: $subfolderName"
				for reports in "$subfile"/*; do
					echo "/////  Reports path: $reports /////"
					#Validates that it is not empty					
					interReport=${reports#*'/mnt/s3bucket/ProcessedLogs/'}
					subInterReport=${interReport#*'/'}
					reportName=${subInterReport#*'/'}
					echo "Report Name: $reportName"
						if [ "$reportName" != "*" -a "$reportName" != "part-r-00000" -a "$reportName" != "_SUCCESS" ]; then
						echo "Table Name: $subfolderName"
						input="LOAD DATA LOCAL INFILE '"$reports"' INTO TABLE $subfolderName;"
						echo "Input: $input"
						mysql -u analytics --password=Password! -h beehiveanalytics.cgyf2jfpme6o.us-east-1.rds.amazonaws.com --port 3306 akndashboardNewVersionTest -e "$input"
						mkdir "$bucket"StoredLogs/`date +"%Y%m%d"`
						mkdir "$bucket"StoredLogs/`date +"%Y%m%d"`/toDatabase
						mv $reports "$bucket"StoredLogs/`date +"%Y%m%d"`/toDatabase/$reportName+`date +"%Y%m%d%M%S"`
						echo "Moved: $reports "$bucket"StoredLogs/`date +"%Y%m%d"`/toDatabase/$reportName+`date +"%Y%m%d%M%S"`"
					fi
				done
				rm -r $subfile
			echo	" "	
		done
	else
		if [ "$intermediate" == "ExtraInformation" ]; then
			mkdir $file/NewLogs $file/SessionFilterNotReady $file/RegsToEmail $file/ParsedLogs
			for subfile in "$file"/*; do	
				for reports in "$subfile"/*; do
					superior=${subfile#*'/mnt/s3bucket/ProcessedLogs/'}
					intermediate=${superior#*'/'}
					intermediateFile=${intermediate%%'/'*}
					filename=${intermediate#*'/'}
					echo "INT. FILENAME: $intermediateFile FILENAME: $filename"
					mkdir "$bucket"StoredLogs/`date +"%Y%m%d"`
					mkdir "$bucket"StoredLogs/`date +"%Y%m%d"`/$intermediateFile
					mv $reports "$bucket"StoredLogs/`date +"%Y%m%d"`/$intermediateFile/$filename+`date +"%Y%m%d%M%S"`
					echo "MOVED: $reports "$bucket"StoredLogs/`date +"%Y%m%d"`/$intermediateFile/$filename+`date +"%Y%m%d%M%S"` "
				done
				rm -r $subfile
			done
			mv $file
		else
			if [ "$intermediate" == "LogsProcessed" ]; then
			echo "LOG PROCESSED FOLDER"
				for subfile in "$file"/*; do 
					interReport=${subfile#*'/mnt/s3bucket/ProcessedLogs/'}
                                	subInterReport=${interReport#*'/'}
                                	archiveName=${subInterReport#*'/'}
					mkdir "$bucket"StoredLogs/`date +"%Y%m%d"`
					mkdir "$bucket"StoredLogs/`date +"%Y%m%d"`/$intermediate
					mv $subfile "$bucket"StoredLogs/`date +"%Y%m%d"`/$intermediate/$archiveName
                                	echo "$subfile "$bucket"StoredLogs/`date +"%Y%m%d"`/$intermediate/$archiveName"
				done
			fi
		fi
	fi
done	
